package com.gemme.gemme.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alipay.sdk.app.AuthTask;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.wode.JiMeiBean;
import com.gemme.gemme.wode.TestAlipayActivity;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangcheng
 * @CreateDate: 2020/8/26 11:19
 */
public class ShouQuanUtil  {

    public static void getWeiXinOpenId(Context mContext,OnShouquan onShouquan){
        //获取微信授权
        UMShareAPI.get(mContext).getPlatformInfo((Activity)mContext, SHARE_MEDIA.WEIXIN, new UMAuthListener(){

            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                System.out.println("");
                System.out.println("");
//                        et_accessToken.setText();
                String weixin_accessToken = map.get("accessToken") + "";
                String weixin_openid = map.get("openid") + "";
                SPUtil.PutData(mContext,Constant.weixin_openid,weixin_openid);

                if(onShouquan != null){
                    onShouquan.onShouquan(weixin_openid,"WECHAT");
                }

                System.out.println("");


            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                System.out.println("");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {

            }
        });
    }


    public static void alipay(Activity mContext, OnShouquan onShouquan){
        SharedPreferences MSP = mContext.getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();
        if(TextUtils.isEmpty(access_token))
            return;
        String url = DataManager.BaseUrl + "user/thirdaccount/aliauth?type=ALI";
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,"Bearer " + access_token);

        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {
                try {
                    JiMeiBean jiMeiBean = JSON.parseObject(response, JiMeiBean.class);
                    System.out.println("");
                    String PID = jiMeiBean.getData().getPid();
                    String APPID = jiMeiBean.getData().getAppid();
                    String RSA2_PRIVATE = jiMeiBean.getData().getRsa2_PRIVATE();
                    alishouquan(mContext,PID,APPID,RSA2_PRIVATE,onShouquan);
                    // shouquan();
                }catch (Exception e){

                }

            }
        });
    }

    private static void alishouquan(Activity mContext,String PID, String APPID, String RSA2_PRIVATE, OnShouquan onShouquan) {
        if(TextUtils.isEmpty(PID) ||
                TextUtils.isEmpty(APPID) ||
                TextUtils.isEmpty(RSA2_PRIVATE)){
            return;
        }
        String TARGET_ID = System.currentTimeMillis() + "";
        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> authInfoMap = OrderInfoUtil2_0.buildAuthInfoMap(PID, APPID, TARGET_ID, rsa2);
        String info = OrderInfoUtil2_0.buildOrderParam(authInfoMap);

//        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
        String privateKey = RSA2_PRIVATE;
        String sign = OrderInfoUtil2_0.getSign(authInfoMap, privateKey, rsa2);
        final String authInfo = info + "&" + sign;
        Runnable authRunnable = new Runnable() {

            @Override
            public void run() {
                // 构造AuthTask 对象

                AuthTask authTask = new AuthTask(mContext);
                // 调用授权接口，获取授权结果
                Map<String, String> result = authTask.authV2(authInfo, true);
                String[] results = (result.get("result")).split("&");
                try {
                    for (int i = 0; i < results.length; i++) {
                        String data = results[i];
                        if(data.contains("alipay_open_id=")){
                            String jieguo = data.substring("alipay_open_id=".length());
                            SPUtil.PutData(mContext,Constant.alipay_open_id,jieguo);
                            if(onShouquan != null){
                                onShouquan.onShouquan(jieguo,"ALIPAY");

                            }

                        }

                    }

                }catch (Exception e){

                }

                System.out.println("");
               /* Message msg = new Message();
                msg.what = SDK_AUTH_FLAG;
                msg.obj = result;
                mHandler.sendMessage(msg);*/
            }
        };

        // 必须异步调用
        Thread authThread = new Thread(authRunnable);
        authThread.start();
    }


    public interface OnShouquan{
        void onShouquan(String shouquan,String type);
    }
}
